//
//  CCCommentModel.m
//  Activity
//
//  Created by its on 15/5/21.
//  Copyright (c) 2015年 coco. All rights reserved.
//

#import "CCCommentModel.h"
#import <LinqToObjectiveC/NSArray+LinqExtensions.h>
@interface CCCommentModel ()
@end

@implementation CCCommentModel
- (RACSignal *) getCommentList:(int)limit activity:(NSString *)activity {
  return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
    NSString *acq = [NSString stringWithFormat:@"select include author.objectId, include author.username, include author.headImage, include parent.author.objectId, include parent.author.username, * from comment  where activity = ? limit ? order by objectId desc"];
    NSArray *pvalue = @[activity, @(limit)];
    @weakify(self);
    @weakify(subscriber);
    [AVQuery doCloudQueryInBackgroundWithCQL:acq pvalues:pvalue callback:^(AVCloudQueryResult *result, NSError *error) {
      @strongify(subscriber);
      @strongify(self);
      if (!error) {
        NSArray *array = [result.results linq_select:^id(AVObject *item) {
          return [item dictionaryForObject];
        }];
        array = [self objectForResponse:array andKlass:[CCComment class] error:NULL];
        [subscriber sendNext: array];
      } else {
        [subscriber sendError:ERROR(3003)];
      }
      
    }];
    return [RACDisposable disposableWithBlock:^{
      [subscriber sendCompleted];
    }];

  }];
}
- (RACSignal *) getCommentList:(int)limit activity:(NSString *)activity after:(NSString *)after {
  return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
    NSString *acq = [NSString stringWithFormat:@"select include author.objectId, include author.username, include author.headImage, include parent.author.objectId, parent.author.username, * from comment  where activity = ? and objectId < ? limit ? order by -objectId"];
    NSArray *pvalue = @[activity, after, @(limit)];
    @weakify(self);
    @weakify(subscriber);
    [AVQuery doCloudQueryInBackgroundWithCQL:acq pvalues:pvalue callback:^(AVCloudQueryResult *result, NSError *error) {
      @strongify(subscriber);
      @strongify(self);
      if (!error) {
        NSArray *array = [result.results linq_select:^id(AVObject *item) {
          return [item dictionaryForObject];
        }];
        array = [self objectForResponse:array andKlass:[CCComment class] error:NULL];
        [subscriber sendNext: array];
      } else {
        [subscriber sendError:ERROR(3003)];
      }
    }];
    return [RACDisposable disposableWithBlock:^{
      [subscriber sendCompleted];
    }];
    
  }];
}

- (RACSignal *) publicComment:(CCPublicComment *) comment {
  return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
    AVObject *object = [AVObject objectWithClassName:@"comment"];
    [object setObject:comment.author forKey:@"author"];
    [object setObject:comment.parent forKey:@"parent"];
    [object setObject:comment.content forKey:@"content"];
    [object setObject:comment.activity forKey:@"activity"];
    [object setObject:comment.upArray forKeyedSubscript:@"upArray"];
    [object setObject:@(comment.upCount) forKeyedSubscript:@"upCount"];
    @weakify(subscriber);
    [object saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
      @strongify(subscriber);
      if (succeeded) {
        [subscriber sendNext:@{@"error":ERROR(3000), @"objectid": object.objectId}];
      } else {
        [subscriber sendNext:@{@"error":ERROR(3001), @"objectid": object.objectId}];
      }
    }];
    return [RACDisposable disposableWithBlock:^{
      [subscriber sendCompleted];
    }];
  }];
}

- (RACSignal *) publicPrivateMessage:(NSDictionary *)dic userid:(NSString *)userid {
  return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
    AVStatus *status = [[AVStatus alloc] init];
    status.data = dic;
    status.type = @"reply";
    AVQuery *query = [AVQuery queryWithClassName:@"_User"];
    [query whereKey:@"objectId" equalTo:userid];
    [status setQuery:query];
    [status sendInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
      
    }];
    return [RACDisposable disposableWithBlock:^{
      
    }];
  }];
}

- (RACSignal *) publicCommentMessage:(NSDictionary *) dic userid:(NSString *)userid {
  return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
    AVStatus *status = [[AVStatus alloc] init];
    status.data = dic;
    status.type = @"comment";
    AVQuery *query = [AVQuery queryWithClassName:@"_User"];
    [query whereKey:@"objectId" equalTo:userid];
    [status setQuery:query];
    [status sendInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
      
    }];
    return [RACDisposable disposableWithBlock:^{
      
    }];
  }];
}

- (id)objectForResponse:(id)response andKlass:(Class)klass error:(NSError **)error {
  id object = [response isKindOfClass:[NSArray class]] ?
  [MTLJSONAdapter modelsOfClass:klass fromJSONArray:response error:error] :
  [MTLJSONAdapter modelOfClass:klass fromJSONDictionary:response error:error];
  
  return object;
}


@end


@implementation CCComment

+ (NSDictionary *) JSONKeyPathsByPropertyKey {
  return @{
           @"author":@"author.username",
           @"authorid":@"author.objectId",
           @"head":@"author.headImage",
           @"commentAuthor":@"parent.author.username",
           @"commentAuthorId":@"parent.author.objectId",
           @"comment":@"content",
           @"objectid":@"objectId",
           @"upid":@"upArray",
           @"upCount":@"upCount"
           };
}

- (RACSignal *) up {
  return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
    AVQuery *query = [AVQuery queryWithClassName:@"comment"];
    AVObject *comment = [query getObjectWithId:self.objectid];
    [comment incrementKey:@"upCount"];
    @weakify(subscriber);
    [comment saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
      @strongify(subscriber);
      if (succeeded) {
        [subscriber sendNext:ERROR(3004)];
        self.upCount = @([_upCount intValue] + 1);
      } else {
        [subscriber sendNext:ERROR(3005)];
      }
    }];
    return [RACDisposable disposableWithBlock:^{
      [subscriber sendCompleted];
    }];
  }];
}


@end
@implementation CCPublicComment

- (NSString *) description {
  return [NSString stringWithFormat:@"%@", self.content];
}
@end